/*
 * SetUI.java
 *
 * Created on September 16, 2006, 8:16 PM
 */

package com.thornapple.setmanager;

import ca.odell.glazedlists.EventList;
import com.thornapple.setmanager.action.RemoveSongAction;
import com.thornapple.setmanager.action.SaveSongAction;
import java.util.Arrays;
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author  Bill
 */
public class SetUI extends javax.swing.JPanel {
    
    EventList songs;
    EventList songSets;
    
    /** Creates new form SetUI */
    public SetUI() {
        initComponents();
    }
    
    public void setSongSetList(final EventList songSets){
        this.songSets = songSets;
        setsForm1.setSongSets(songSets);
        songBrowseForm1.setSongSets(songSets);
    }
    
    public void setSongList(final EventList songs){
        this.songs = songs;
        songBrowseForm1.setSongs(songs);
        importSongForm1.setSongs(songs);
        
        songBrowseForm1.getSongListComponent().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting())return;
                Song song = (Song) songBrowseForm1.getSongListComponent().getSelectedValue();
                songForm1.setSong(song);
            }
        });
        setsForm1.getSongListComponent().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting())return;
                Song song = (Song) setsForm1.getSongListComponent().getSelectedValue();
                songForm1.setSong(song);
            }
        });
        importSongForm1.getSongListComponent().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                Object selected = 
                        importSongForm1.getSongListComponent().getSelectedValue();
                Song song = null;
                if (selected instanceof Song)
                    song = (Song)selected;
                else 
                    return;
                songForm1.setSong(song);
            }
        });
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        songForm1 = new com.thornapple.setmanager.SongForm();
        navPane = new javax.swing.JTabbedPane();
        songBrowseForm1 = new com.thornapple.setmanager.SongBrowseForm();
        importSongForm1 = new com.thornapple.setmanager.ImportSongForm();
        setsForm1 = new com.thornapple.setmanager.SetsForm();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        songForm1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        navPane.addTab("My Library", songBrowseForm1);

        navPane.addTab("Add Songs", importSongForm1);

        navPane.addTab("My Sets", setsForm1);

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Remove");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(navPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 296, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(songForm1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jButton2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton1)
                        .add(18, 18, 18))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(navPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(songForm1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        List oldSongs = Arrays.asList(
                songBrowseForm1.getSongListComponent().getSelectedValues());
        new RemoveSongAction(songs,oldSongs).actionPerformed(null);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        List newSongs = null;
        if (navPane.getSelectedIndex() == 1) 
            newSongs = importSongForm1.getSelectedSongs();
        else 
            newSongs = songBrowseForm1.getSelectedSongs();
        new SaveSongAction(songs,newSongs).actionPerformed(null);
        //TODO delete a bunch at once
        //TODO move songs to another artist
    }//GEN-LAST:event_jButton1ActionPerformed
            
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.thornapple.setmanager.ImportSongForm importSongForm1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTabbedPane navPane;
    private com.thornapple.setmanager.SetsForm setsForm1;
    private com.thornapple.setmanager.SongBrowseForm songBrowseForm1;
    private com.thornapple.setmanager.SongForm songForm1;
    // End of variables declaration//GEN-END:variables
    
}
